﻿using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Threading;

using COUP.Core.Enums;

namespace COUP.Core.Files
{
    public static class MonsterType
    {
        public static Dictionary<string, MonsterTypeEntry> Parse()
        {
            Dictionary<string, MonsterTypeEntry> _MonsterData = new Dictionary<string, MonsterTypeEntry>();

            while (!Monitor.TryEnter(Console.Out, 50)) { }
            Console.Write("[Game Server] Loading Monster Data...");
            int Left = Console.CursorLeft;
            FileStream File = new FileStream(Settings.MonsterType, FileMode.Open);
            BinaryReader Reader = new BinaryReader(File);

            int start = System.Environment.TickCount;
            uint Amount = Reader.ReadUInt32();

            MonsterTypeEntry _MDetail;

            for (int i = 0; i < Amount; i++)
            {
                _MDetail = new MonsterTypeEntry();
                uint NameLength = Reader.ReadUInt32();

                for (int x = 0; x < NameLength; x++)
                {
                    _MDetail.Name += (char)Reader.ReadByte();
                }
                _MDetail.Name = _MDetail.Name.Trim('\0');

                Console.Write(_MDetail.Name);
                Console.CursorLeft = Left;

                _MDetail.SizeAdd = Reader.ReadUInt32();
                _MDetail.Zoom = Reader.ReadUInt32();
                _MDetail.HP = Reader.ReadUInt32();
                _MDetail.Level = (byte)Reader.ReadUInt32();

                if (!_MonsterData.ContainsKey(_MDetail.Name))
                    _MonsterData.Add(_MDetail.Name, _MDetail);
            }

            Console.Write("Completed in {0}ms\n", Environment.TickCount - start);
            Monitor.Exit(Console.Out);

            File.Dispose();
            File.Close();
            Reader.Close();

            return _MonsterData;
        }
    }
}
